package server;


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

public class CLassTest  {

    public static void main(String[] args) throws Exception {
//
//        String path = "/demo1/index.html";
//        path.substring(path.indexOf("/",2));
//        System.out.println(path.substring(path.indexOf("/",2)));
//        CLassTest cLassTest = new CLassTest();
//
//        File file = new File("D:\\project\\Minicat\\webapps\\demo1.war");
//        URL url = file.toURI().toURL();
//        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
//        //4.通过urlClassLoader加载器调用loadClass方法传入类名动态加载class文件并获取class对象:会初始化静态块
//        Class<?> classs = urlClassLoader.loadClass("server.LagouServlet");
//
//        //5.通过class对象创建实例
//        Object newInstance = classs.newInstance();
//        System.out.println(newInstance);


//        InputStream resourceAsStream = null;
//        SAXReader saxReader = new SAXReader();
//
//        try {
//            resourceAsStream = new FileInputStream("D:\\project\\Minicat\\webapps\\demo1\\web.xml");
//            Document document = saxReader.read(resourceAsStream);
//            Element rootElement = document.getRootElement();
//            List<Element> selectNodes = rootElement.selectNodes("//servlet");
//            for (int i = 0; i < selectNodes.size(); i++) {
//                Element element =  selectNodes.get(i);
//                // <servlet-name>lagou</servlet-name>
//                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
//                String servletName = servletnameElement.getStringValue();
//                // <servlet-class>server.LagouServlet</servlet-class>
//                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
//                String servletClass = servletclassElement.getStringValue();
//                System.out.println(servletClass);
//
//                // 根据servlet-name的值找到url-pattern
//                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
//                // /lagou
//                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
//                System.out.println(urlPattern);
//
//            }
//        }catch(Exception e){
//
//        }

//        cLassTest.test1();

    }

    private void test1(){
//        InputStream serverStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();
        try{
            InputStream serverStream = new FileInputStream("D:\\project\\Minicat\\src\\main\\resources\\server.xml");
            Document read = saxReader.read(serverStream);
            Element rootElement = read.getRootElement();
            Element conClement1 = rootElement.element("Service");
            System.out.println("======"+conClement1.attributeValue("name"));
            Element conClement = (Element) rootElement.selectSingleNode("//Connector");
            int port = Integer.parseInt(conClement.attributeValue("port"));
            System.out.println("======"+port);
            Element hostElement = (Element) rootElement.selectSingleNode("//Host");
            String appBase = hostElement.attributeValue("appBase");
            System.out.println("======"+appBase);
            List<Element> listContext = read.selectNodes("//Context");
            for (Element element : listContext) {
                String docBase = element.attributeValue("docBase");
                String path = element.attributeValue("path");
                System.out.println(docBase+"======"+path);
            }

        }catch(Exception e){

        }
    }
}
